//
//  SinaNetWorkTools.swift
//  mySina
//
//  Created by 刘莹 on 2016/11/13.
//  Copyright © 2016年 liuying. All rights reserved.
//

import UIKit
import AFNetworking
//MARK: -
//MARK: - 声明枚举
enum HTTPMethod: Int{

    case GET = 0
    case POST = 1
}
class SinaNetWorkTools: AFHTTPSessionManager {
    //MARK: -
    //MARK: - 声明单例
    static let shared:SinaNetWorkTools = {
    
        let tools = SinaNetWorkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    //MARK: -
    //MARK: - 声明封装的网络方法
    
    func request(method:HTTPMethod,urlString:String,parameter:Any?,finished:@escaping (Any?,Error?) -> ()){
        if method == .GET {
            self.get(urlString, parameters: parameter, progress: nil, success: { (task, response) in
                finished(response,nil)
                }, failure: { (task, error) in
                    finished(nil,error)
            })
            
        }else{
        
            self.post(urlString, parameters: parameter, progress: nil, success: { (task, response) in
                finished(response,nil)
                }, failure: { (task, error) in
                    finished(nil,error)
            })
        }
    
    
    }
}
